package com.lanswon.qzsmk.controller.cardManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.PathContants;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.CardNoGenerated;
import com.lanswon.qzsmk.model.CardNoUseDetail;
import com.lanswon.qzsmk.service.cardManageService.BanckCardBusinessService;
import com.lanswon.qzsmk.service.cardManageService.MakeCardService;
import com.lanswon.qzsmk.util.CitizenCardNoGeneratorUtil;
import com.lanswon.qzsmk.vo.CardNoGeneratedVo;
import com.lanswon.qzsmk.vo.ImportBankFileVo;
import com.lanswon.qzsmk.util.FileUtil;

@Controller
@RequestMapping(value="/fileEntering")
public class FileEnterController  {
	
	@Autowired
	MakeCardService mcs;
	
	@Autowired
	BanckCardBusinessService bcs;
	
	@RequiresPermissions("fileEntering:index")
	@RequestMapping(value="/index",name="文件录入,fileEntering:index",method={RequestMethod.GET,RequestMethod.POST})
	public String index(){
		return "FileEnter";
	}
	
	@RequiresPermissions("fileEntering:goGenerateCardNumber")
	@RequestMapping(value="/goGenerateCardNumber",name="去生成卡号,fileEntering:goGenerateCardNumber",method={RequestMethod.GET,RequestMethod.POST})
	public String goGenerateCardNumber(){
		
		return "GenerateCardNo";
	}
	
	@RequiresPermissions("fileEntering:goGenerateCardNoDetail")
	@RequestMapping(value="/goGenerateCardNoDetail",name="去查看卡号生成详情,fileEntering:goGenerateCardNoDetail",method={RequestMethod.GET,RequestMethod.POST})
	public String goGenerateCardNoDetail(){
		
		return "CardNoDetail";
	}
	
	@RequiresPermissions("fileEntering:showBankFileList")
	@ResponseBody
	@RequestMapping(value="/showBankFileList",name="展示文件列表,fileEntering:showBankFileList",method={RequestMethod.POST})
	public DwzPage<ImportBankFileVo> showBankFileList(ImportBankFileVo vo,Integer pageNum,Integer numPerPage) throws ApplicationException{
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		vo.setCurrentPage(pageNum);
		vo.setNumPerPage(numPerPage);
		vo.setStartLine((vo.getCurrentPage()-1)*vo.getNumPerPage());
		DwzPage<ImportBankFileVo> dPage=bcs.showBankFileList(vo);
		return dPage;	
	}
	
	@RequiresPermissions("fileEntering:importBankFile")
	@ResponseBody
	@RequestMapping(value="/importBankFile",name="导入银行文件,fileEntering:importBankFile",method=RequestMethod.POST)
	public Map<String, Object> importBankFile(ImportBankFileVo vo) throws Exception{
		Map<String, Object> map=new HashMap<>();
		for(int i=0;i<vo.getFileNames().length;i++){
			System.out.println(vo.getFileNames()[i]);
			vo.setFileName(vo.getFileNames()[i]);
			bcs.readFileInfoByBank(vo);
		}		
		map.put("message", "导入成功");
		map.put("statusCode", "200");
		return map;
	}
	
	@RequiresPermissions("fileEntering:generateCardNumber")
	@RequestMapping(value="/generateCardNumber",name="生成卡号,fileEntering:generateCardNumber",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>  generateCardNumber(String cardPCode,String cardPhysicalType,String cardNoNUm,HttpServletResponse response) throws NumberFormatException, Exception{
		Map<String, Object>map=new HashMap<>();
		String prefix = CitizenCardNoGeneratorUtil.getCardNoPrefix(cardPCode, cardPhysicalType);
		Boolean f=bcs.generatorCitizenCardNo(cardPCode,prefix, Integer.valueOf(cardNoNUm));
		if(f) {
			map.put("statusCode", 200);
			map.put("message", PathContants.FTP_URL+PathContants.FTP_PATH_GENERATEDCARDNO);
		}
		else {
			map.put("statusCode", 300);
			map.put("message", "生成卡号上传ftp失败");
		}
		return map;
	}
	
	@RequiresPermissions("fileEntering:queryCardNoDetail")
	@RequestMapping(value="/queryCardNoDetail",name="查询卡号批量生成记录,fileEntering:queryCardNoDetail",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<CardNoGeneratedVo> queryCardNoDetail(CardNoGeneratedVo vo, Integer pageNum,Integer numPerPage){
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		System.out.println(vo);
		vo.setCurrentPage(pageNum);
		vo.setNumPerPage(numPerPage);
		vo.setStartLine((vo.getCurrentPage()-1)*vo.getNumPerPage());
		System.out.println(vo);
		DwzPage<CardNoGeneratedVo> dPage=bcs.queryCardNoDetail(vo);
		
		return dPage;
	}
	
	@RequestMapping(value="/goCardNoUseDetail",name="去查询卡号使用详情页面,fileEntering:goCardNoUseDetail",method=RequestMethod.GET)
	public String goCardNoUseDetail(CardNoUseDetail cardNoUseDetail,HttpServletRequest request){
		System.out.println(cardNoUseDetail);
		List<CardNoUseDetail> cardNoUseDetails=bcs.queryAllRecord(cardNoUseDetail);
		request.getSession().setAttribute("cardNoUseDetails", cardNoUseDetails);
		return "CardNoUseDetail";
	}
	
	
	@RequestMapping(value="/showCardNoUseDetail",name="展示卡号使用情况,fileEntering:showCardNoUseDetail",method=RequestMethod.POST)
	@ResponseBody
	public List<CardNoUseDetail> showCardNoUseDetail(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		List<CardNoUseDetail> cardNoUseDetails=(List<CardNoUseDetail>) request.getSession().getAttribute("cardNoUseDetails");
		return cardNoUseDetails;
	}
	
}
